package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.OrderHistory;
import com.ruoyi.system.domain.SceneBook;
import com.ruoyi.system.domain.Scenicarea;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SceneBookMapper;
import com.ruoyi.system.mapper.ScenicAreaMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISceneBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 景区门票预订Service业务层处理
 * 
 * @author xdh
 * @date 2024-06-26
 */
@Service
public class SceneBookServiceImpl implements ISceneBookService
{
    @Autowired
    private SceneBookMapper sceneBookMapper;
    @Autowired
    private OrderHistoryServiceImpl orderHistoryService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private ScenicAreaMapper scenicAreaMapper;
    /**
     * 查询景区门票预订
     * 
     * @param scenicBookingId 景区门票预订主键
     * @return 景区门票预订
     */
    @Override
    public SceneBook selectSceneBookByScenicBookingId(Integer scenicBookingId)
    {
        return sceneBookMapper.selectSceneBookByScenicBookingId(scenicBookingId);
    }

    /**
     * 查询景区门票预订列表
     * 
     * @param sceneBook 景区门票预订
     * @return 景区门票预订
     */
    @Override
    public List<SceneBook> selectSceneBookList(SceneBook sceneBook)
    {
        Long userId= SecurityUtils.getUserId();
        SysUserRole sysUserRole= sysUserRoleMapper.getUserRole(userId);
        if(sysUserRole.getRoleId()==101){   //101表示为游客的角色，只能看到游客自己的订单
            sceneBook.setUserId(Integer.valueOf(userId.toString()));
        }
        return sceneBookMapper.selectSceneBookList(sceneBook);
    }

    /**
     * 新增景区门票预订
     * 
     * @param sceneBook 景区门票预订
     * @return 结果
     */
    @Override
    public int insertSceneBook(SceneBook sceneBook)
    {
        return sceneBookMapper.insertSceneBook(sceneBook);
    }

    /**
     * 修改景区门票预订
     * 
     * @param sceneBook 景区门票预订
     * @return 结果
     */
    @Override
    public int updateSceneBook(SceneBook sceneBook)
    {
        if(sceneBook.getPaymentStatus()==1) {
            OrderHistory orderHistory = new OrderHistory();
            orderHistory.setUserId(Long.valueOf(sceneBook.getUserId()));
            orderHistory.setBookingId(Long.valueOf(sceneBook.getScenicBookingId()));
            orderHistory.setOrderDate(new Date());

            Scenicarea scenicarea = scenicAreaMapper.selectScenicareaByAreaId(sceneBook.getAreaId());
            String detail = "这是景区门票预订的订单，景区是" + scenicarea.getAreaName() + ",人数为：" + sceneBook.getTripCount() + "。";
            orderHistory.setOrderDetail(detail);
            orderHistory.setOrderPrice(Long.valueOf(sceneBook.getTotalPrice()));
            orderHistoryService.insertOrderHistory(orderHistory);
        }
        return sceneBookMapper.updateSceneBook(sceneBook);
    }

    /**
     * 批量删除景区门票预订
     * 
     * @param scenicBookingIds 需要删除的景区门票预订主键
     * @return 结果
     */
    @Override
    public int deleteSceneBookByScenicBookingIds(Integer[] scenicBookingIds)
    {
        return sceneBookMapper.deleteSceneBookByScenicBookingIds(scenicBookingIds);
    }

    /**
     * 删除景区门票预订信息
     * 
     * @param scenicBookingId 景区门票预订主键
     * @return 结果
     */
    @Override
    public int deleteSceneBookByScenicBookingId(Integer scenicBookingId)
    {
        return sceneBookMapper.deleteSceneBookByScenicBookingId(scenicBookingId);
    }
}
